//
//  AddContactViewController.m
//  18-HomeWork-添加联系人
//
//  Created by dllo on 16/8/6.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "AddContactViewController.h"
#import "Contacts.h"
@interface AddContactViewController ()<UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textFieldName;
@property (weak, nonatomic) IBOutlet UITextField *testFieldPhoneNumber;
@property (weak, nonatomic) IBOutlet UITextField *textFieldAddress;
@property (weak, nonatomic) IBOutlet UIImageView *addHeadImage;
@property (nonatomic, strong) NSData *imageData;


@end

@implementation AddContactViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.textFieldAddress.delegate = self;
    self.textFieldName.delegate = self;
    self.testFieldPhoneNumber.delegate = self;
}
- (IBAction)clickAddButton:(id)sender {
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *avatarDirectory = [[pathArray firstObject] stringByAppendingPathComponent:@"avatarsDirectory"];
    NSFileManager *manager = [NSFileManager defaultManager];
    [manager createDirectoryAtPath:avatarDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    Contacts *con = [[Contacts alloc] init];
    con.nameString = _textFieldName.text;
    con.phoneNumber = _testFieldPhoneNumber.text;
    con.headImageString = _testFieldPhoneNumber.text;
    con.addressString = _textFieldAddress.text;
    NSString *imagePath = [avatarDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", _textFieldName.text]];
    NSLog(@"add-----%@", imagePath);
    [_imageData writeToFile:imagePath atomically:YES];
    con.headImageString = imagePath;
    if (self.delegate && [self.delegate respondsToSelector:@selector(addStudent:)]) {
        [self.delegate addStudent:con];
    }
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (IBAction)chooseImage:(id)sender {
    [self getImageFromIpc];
    
}
- (void)getImageFromIpc
{
    // 1.判断相册是否可以打开
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
    // 2. 创建图片选择控制器
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
      // 3. 设置打开照片相册类型(显示所有相簿)
    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    // 4.设置代理
    ipc.delegate = self;
    // 5.modal出这个控制器
    [self presentViewController:ipc animated:YES completion:nil];
}

#pragma mark -- <UIImagePickerControllerDelegate>--
// 获取图片后的操作
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    // 销毁控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    // 设置图片
    self.addHeadImage.image = info[UIImagePickerControllerOriginalImage];
    NSData *data;
    if (UIImagePNGRepresentation(self.addHeadImage.image) == nil) {
        data = UIImageJPEGRepresentation(self.addHeadImage.image, 1);
    } else {
        data = UIImagePNGRepresentation(self.addHeadImage.image);
    }
    self.imageData = [NSData data];
    _imageData = data;
}








- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField endEditing:YES];
    return YES;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.textFieldName endEditing:YES];
    [self.testFieldPhoneNumber endEditing:YES];
    [self.textFieldAddress endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
